using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using _7tc056_QuanLyNhaSach.HeThongLuuTru;
using _7tc056_QuanLyNhaSach.HeThongXuLy;

namespace _7tc056_QuanLyNhaSach.HeThongLuuTru
{
    class SachDAO
    {
        static public DataTable SelectSachLikeName(SachDTO s)
        {
            string sql = "select * from SACH where TenSach like '%" + s.TenSach + "%'";
            return DataAssecc.ExecQuery(sql);
        }
        static public DataTable SelectSachLikeMaTheLoai(SachDTO s)
        {
            string sql = "select * from SACH where TheLoai like '%" + s.TheLoai + "%'";
            return DataAssecc.ExecQuery(sql);
        }

        static public DataTable SelectSachLikeTacGia(SachDTO s)
        {
            string sql = "select * from SACH where TacGia like '%" + s.TacGia + "%'";
            return DataAssecc.ExecQuery(sql);
        }
        static public DataTable SelectSachAll()
        {
            string sql = "select* from SACH";
            DataTable dt = DataAssecc.ExecQuery(sql);
            return dt;
        }
        static public void Insert(SachDTO s)
        {
            string sql = "insert into SACH(TenSach,MaTheLoai,TacGia,SoLuong,GiaBan) values('" + s.TenSach + "'," + s.TheLoai + ",'" + s.TacGia + "'," + s.SoLuong + "," + s.GiaBan + ")";
            DataAssecc.ExecNonQuery(sql);

        }
        public static bool Delete(SachDTO s)
        {
            try
            {
                string sql = "delete from SACH where MaSach= " + s.MaSach + "";
                DataAssecc.ExecNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }
        static public void Update(SachDTO s)
        {
            string sql = "update  SACH set TenSach=('" + s.TenSach + "'),MaTheLoai=(" + s.TheLoai + "),TacGia=('" + s.TacGia + "'),SoLuong=(" + s.SoLuong + "), GiaBan=(" + s.GiaBan + ")where MaSach = " + s.MaSach + "";
            DataAssecc.ExecNonQuery(sql);
        }
        static public void UpdateSoLuongTon(SachDTO s)
        {
            string sql = "update SACH set SoLuong=(" + s.SoLuong + ") where MaSach = " + s.MaSach + "";
            DataAssecc.ExecNonQuery(sql);
        }
        static public SachDTO SelectTLByName(string name)
        {
            string sql = "select * from SACH where TenSach='" + name + "'";
            DataTable dt = DataAssecc.ExecQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                SachDTO s = new SachDTO();
                s.TenSach = dt.Rows[0].ItemArray[0].ToString();
                return s;
            }
        }
        static public SachDTO SelectSachTheoMa(int ma)
        {
            string sql = "select * from SACH where MaSach=" + ma + "";
            DataTable dt = DataAssecc.ExecQuery(sql);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                SachDTO s = new SachDTO();
                s.MaSach = int.Parse(dt.Rows[0].ItemArray[0].ToString());
                return s;
            }
        }
        public static DataTable GetSachCondition(int SoLuong)
        {
            string sql = "select * from SACH where SoLuong < " + SoLuong;
            return DataAssecc.ExecQuery(sql);
        }

        internal static object SelectPNByName(object p)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        

    }
}
